package load;



import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import model.DayPlanModel;
import model.HouseModel;
import model.Interval;
import model.ObjectModel;
import model.PersonModel;
import model.RoomModel;
import model.Sex;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;



public class XMLtoModel {

	public static void loadHouse(File file) {


		 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		 DocumentBuilder db;
		try {
			   db = dbf.newDocumentBuilder();
			  Document doc = db.parse(file);
			  doc.getDocumentElement().normalize();
			  
			  NodeList nodeList = doc.getElementsByTagName("House");
			  nodeList = doc.getElementsByTagName("Room");
			  for (int i = 0; i < nodeList.getLength(); i++) {
				RoomModel next=buildRoom(nodeList.item(i));
				HouseModel.roomList.add(next);			
			  }
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		 
	 }
	
	private static RoomModel buildRoom(Node n)
	{
		RoomModel nextRoom;
		NamedNodeMap attributeList = n.getAttributes();
		Node type=attributeList.getNamedItem("Type");
		Node name=attributeList.getNamedItem("Name");
		nextRoom=new RoomModel(name.getNodeValue(),type.getNodeValue());
		NodeList list = n.getChildNodes();
		for(int i=0;i<list.getLength();i++)
		{
			Node nextChild=list.item(i);
			if(nextChild.getNodeName().equalsIgnoreCase("Object"))
			{
					ObjectModel object=buildObject(nextChild,nextRoom);
					nextRoom.add(object);
				
		}
		}
			
	
		return nextRoom;
	}
	

	
	private static ObjectModel buildObject(Node n,RoomModel currentRoom)
	{

		NamedNodeMap attributeList = n.getAttributes();
		String type=attributeList.getNamedItem("Type").getNodeValue();
		String name=attributeList.getNamedItem("Name").getNodeValue();
		ObjectModel newObject=new ObjectModel(name, type);	
				
		return newObject;
	}

	public static void loadFamily(File file) {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();

			NodeList nodeList = doc.getElementsByTagName("Family");
			nodeList = doc.getElementsByTagName("Person");
			for (int i = 0; i < nodeList.getLength(); i++) {
				PersonModel next = buildPerson(nodeList.item(i));
				PersonModel.list.add(next);
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static PersonModel buildPerson(Node item) {
		PersonModel p = new PersonModel();
		NamedNodeMap attributeList = item.getAttributes();
		/**
		 * Reading Attributes from list;
		 */
		Node first = attributeList.getNamedItem("FirstName");
		Node sex = attributeList.getNamedItem("sex");
		Node age = attributeList.getNamedItem("age");
		p.setName(first.getNodeValue());
		p.setSex(Sex.getByValue(sex.getNodeValue()));
		p.setAge(Integer.parseInt(age.getNodeValue()));
		// System.out.println(item.getNodeName());
		NodeList list = item.getChildNodes();
		if (list != null) {
			for (int i = 0; i < list.getLength(); i++)
				if (list.item(i).getNodeName().equalsIgnoreCase("Schedule")) {
					list = list.item(i).getChildNodes();
					DayPlanModel dpm = new DayPlanModel();
					for (int j = 0; j < list.getLength(); j++) {
						attributeList = list.item(j).getAttributes();
						if (DayPlanModel.isDay(list.item(j).getNodeName())) {
							String leave = attributeList.getNamedItem(
									"LeaveTime").getNodeValue();
							String home = attributeList
									.getNamedItem("HomeTime").getNodeValue();
							dpm.set(list.item(j).getNodeName(), new Interval(
									leave, home));
						}
					}
					p.setDpm(dpm);
					break;
				}
		}
		return p;
	}

}
